<?php


namespace App\Bizs\Mp2;


use App\Exceptions\BizException;
use App\Models\MomentModel;
use App\Models\TopicModel;
use App\Supports\DateSupport;
use Base\BaseBiz;
use Lib\Log;
use Lib\RedisManager;

class MomentBiz extends BaseBiz
{
    public function getTopicList()
    {
        return (new TopicModel())->getAllAvaliableTopic();
    }

    public function sendMoment($params, $uid)
    {
        $redisKey = config('keys.redis.MOMENT_UNIQUE_BY_USER');
        $momentWait = RedisManager::init()->get($redisKey . $uid);
        if ($momentWait > 0) {
            throw new BizException('发帖太频繁啦，请等待' . ($momentWait - time()) . '秒');
        }
        try {
            $insert = (new MomentModel())->insertMoment([
                'topic' => $params['topic'],
                'uid' => $uid,
                'content' => $params['content'],
                'comment_count' => 0,
                'images' => $params['images'],
                'create_time' => DateSupport::nowDate()
            ]);
            self::reCalcMomentCount($params['topic']);
            return [
                'insert' =>$insert
            ];
        } catch (\Exception $e) {
            Log::error("发帖错误：" . $e->getMessage());
            throw new BizException('发帖时出现错误');
        }
    }

    public static function reCalcMomentCount($topic)
    {

    }
}
